package org.example.task.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.example.task.mapper.TaskAssignmentMapper;
import org.example.task.model.dto.taskassignment.TaskAssignDTO;
import org.example.task.model.po.TaskAssignment;
import org.example.task.service.ITaskAssignmentService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;


@Service
@RequiredArgsConstructor
@Transactional
public class TaskAssignmentServiceImpl extends ServiceImpl<TaskAssignmentMapper, TaskAssignment> implements ITaskAssignmentService {
    @Override
    public boolean assignTask(TaskAssignDTO taskAssignDTO) {
        List<TaskAssignment> taskAssignmentList = new ArrayList<>();
        for (Long userId : taskAssignDTO.getUserIds()) {
            // 每次循环创建新对象，避免对象复用
            TaskAssignment taskAssignment = new TaskAssignment();
            taskAssignment.setTaskId(taskAssignDTO.getTaskId());
            taskAssignment.setLeaderId(taskAssignDTO.getLeaderId());
            taskAssignment.setAssignTime(LocalDateTime.now());
            taskAssignment.setUserId(userId);
            taskAssignmentList.add(taskAssignment);
        }

        return saveBatch(taskAssignmentList);
    }
}
